package com.ids.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Set;
import java.util.TreeSet;
import com.ids.bean.UserAttendanceData;
import com.ids.util.Constants;
import com.ids.util.DbManager;

public class AttendanceDao {
	private static AttendanceDao instance = new AttendanceDao();
	private Set<String> result =null;
	public static AttendanceDao getInstance() {
		return instance;
	}
	
public Set<String> insertUserAttendanceData(UserAttendanceData userAttendance , String empCode) {
		result = new TreeSet<String>();
		String updateAttendanceDataSql ="update attendance set attendanceTypeId =? ,dependantAttendanceTypeId =?  where empCode =? and attendanceId=? and statusId != (select statusId from status where statusValue = 'Approved')";
		String insertAttendanceDataSql ="insert into attendance (attendanceTypeId,dependantAttendanceTypeId,empCode,dated,statusId,updationDate) values (?,?,?,?,?,?)";
		DbManager dbManager = new DbManager();
		Connection conn = dbManager.getConnection();
	    try {
			int id = getAttendanceId(userAttendance,empCode, conn);
			if (-1 == id) {
				 insertAttendance(insertAttendanceDataSql,userAttendance,empCode, conn);
			} else {
				updateAttendance(id,updateAttendanceDataSql, userAttendance,empCode, conn);
			}
	   } catch (Exception e) {
			e.printStackTrace();
			result.add(Constants.FAILURE);
			
		} 
		 return result;
		
	}
	    
	    private int getAttendanceId(UserAttendanceData userAttendance,String empCode,Connection conn){
	    	ResultSet rs = null;
			int id = -1;
			Date date = new Date(userAttendance.getDate().getTime());
			String attendanceSql =" select attendanceId from attendance where empCode="+empCode+" and dated='"+date+"'";
			try {
				Statement stmt =(Statement) conn.createStatement();
				rs = stmt.executeQuery(attendanceSql);
				if (rs.next()) {
					id = rs.getInt("attendanceId");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DbManager.closeResultSet(rs);
			}
			return id;
	    }
	    
	    private void insertAttendance(String insertAttendanceDataSql,
	    	UserAttendanceData userAttendance,String empCode, Connection conn) {
	    	int attendanceTypeId = userAttendance.getAttendanceTypeId();
	    	int dependantAttendanceTypeId = userAttendance.getDependantAttendanceTypeId();
		    Date date = new Date(userAttendance.getDate().getTime());
		    int noOfDays = 1;
		    if (attendanceTypeId==Constants.ATTENDANCE_ONE_PL){
		    	noOfDays = userAttendance.getNoOfDays();
		    }
		    for(int i=1 ;  i<=noOfDays ; i++){
		    	if(i>1){
		    	int increment = i;
		    	Calendar cal= Calendar.getInstance();
		    	cal.setTime(userAttendance.getDate());
		    	cal.add(Calendar.DATE, increment-1);
		    	date =new java.sql.Date(cal.getTime().getTime());
		    			
		    	}
		    try{
		    	PreparedStatement prepStmt = conn.prepareStatement(insertAttendanceDataSql);
		    	prepStmt.setInt(1, attendanceTypeId);
		    	prepStmt.setInt(2, dependantAttendanceTypeId);
		    	prepStmt.setString(3, empCode);
		       	prepStmt.setDate(4, date);
		       	prepStmt.setInt(5, userAttendance.getStatusId());
		       	prepStmt.setDate(6, date);
		       	
		        prepStmt.execute();
		        result.add(Constants.SUCCESS);
		     		    	
		    } catch (Exception e) {
				e.printStackTrace();
				result.add(Constants.FAILURE);
			} 
		    }
		}
	    
	    private void updateAttendance(int attendanceId,String insertAttendanceDataSql,
	    	UserAttendanceData userAttendance,String empCode, Connection conn) {
	    	int attendanceTypeId = userAttendance.getAttendanceTypeId();
	    	int dependantAttendanceTypeId = userAttendance.getDependantAttendanceTypeId();
		    try{
		    	PreparedStatement prepStmt = conn
				.prepareStatement(insertAttendanceDataSql);
		    	prepStmt.setInt(1, attendanceTypeId);
		    	prepStmt.setInt(2, dependantAttendanceTypeId);
		    	prepStmt.setString(3, empCode);
		       	prepStmt.setInt(4, attendanceId);
		        prepStmt.execute();
		        result.add(Constants.UPDATE);
		     		    	
		    } catch (Exception e) {
				e.printStackTrace();
				result.add(Constants.FAILURE);
			} 
	  }

}
